﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace HAZ.WinDBConnection.Forms
{
    public partial class ConPreco : UserControl
    {
        private ConnectionFactory.DBConnection dbConnection = new HAZ.ConnectionFactory.DBConnection();

        public ConPreco()
        {
            InitializeComponent();
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            #region Consulta Preço
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    lblPreco.Text = "R$ 0,00";
                    lblObs.Text   = "";
                    lblDesc.Text  = "";
                    imgProd.Image = null;

                    StringBuilder sQuery = new StringBuilder();
                    sQuery.AppendLine("SELECT #top_1# prd.Referencia, prd.DescrProduto");
                    sQuery.AppendLine("     , CASE WHEN IsNull(ppd.Referencia,'') = ''");
                    sQuery.AppendLine("            THEN 'Preço Normal'");
                    sQuery.AppendLine("            ELSE 'PROMOÇÃO !'");
                    sQuery.AppendLine("       END Promocao");
                    sQuery.AppendLine("     , CASE WHEN IsNull(ppd.Referencia,'') = ''");
                    sQuery.AppendLine("            THEN prc.PrecoVarejo");
                    sQuery.AppendLine("            ELSE ppd.VendaPromocao");
                    sQuery.AppendLine("       END Preco");
                    sQuery.AppendLine("  FROM GalProduto prd");
                    sQuery.AppendLine(" INNER JOIN GalPrecos prc");
                    sQuery.AppendLine("    ON prc.Referencia   = prd.Referencia");
                    sQuery.AppendLine("   AND prc.CodRede      = prd.CodRede");
                    sQuery.AppendLine("   AND prc.CodLoja      = '#loja#'");
                    sQuery.AppendLine("  LEFT JOIN PromocaoProduto ppd");
                    sQuery.AppendLine("    ON ppd.CodLoja      = prc.CodLoja");
                    sQuery.AppendLine("   AND ppd.Referencia   = prd.Referencia");
                    sQuery.AppendLine("   AND ppd.CodRede      = prd.CodRede");
                    sQuery.AppendLine("   AND ppd.DataInicial <= GetDate()");
                    sQuery.AppendLine("   AND ppd.DataFinal   >= GetDate()");
                    sQuery.AppendLine(" WHERE prd.Referencia LIKE '#ref#%'");
                    sQuery.AppendLine(" ORDER BY prd.DescrProduto");

                    ConnectionFactory.IDBConnector dbConn = ConnectionFactory.DBFactory.GetConnection(dbConnection.SGBD);

                    string strQuery = sQuery.ToString();
                    strQuery = strQuery.Replace("#top_1#", (primeiro.Checked ? "top 1" : ""));
                    strQuery = strQuery.Replace("#loja#" , dbConnection.CodLoja);
                    strQuery = strQuery.Replace("#ref#"  , txtReferencia.Text.Trim());

                    DataTable dados = dbConn.Select(strQuery);

                    if (dados.DefaultView.Count > 0)
                    {
                        if (dados.DefaultView.Count == 1)
                        {
                            lblPreco.Text = dados.DefaultView[0]["Preco"       ].ToString();
                            lblObs.Text   = dados.DefaultView[0]["Promocao"    ].ToString();
                            lblDesc.Text  = dados.DefaultView[0]["DescrProduto"].ToString();
                        }
                        else
                        {
                            DataGridView dgv = new DataGridView();
                            dgv.DataSource          = dados;
                            dgv.Dock                = DockStyle.Fill;
                            dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
                            dgv.SelectionMode       = DataGridViewSelectionMode.FullRowSelect;
                            dgv.ReadOnly            = true;

                            Form frm = new Form();
                            frm.Text        = "Consulta Preço [Ref: " + txtReferencia.Text.Trim() + "]";
                            frm.WindowState = FormWindowState.Maximized;
                            frm.Font        = new Font("Courier New", 12);

                            frm.Controls.Add(dgv);
                            frm.ShowDialog(this);
                        }
                    }
                    else
                    {
                        lblPreco.Text = "R$ 0,00";
                        lblObs.Text   = "Produto Não Encontrado";
                        lblDesc.Text  = "";
                    }

                    string strFullFileName = dbConnection.ImagesRootPath + "\\P" + txtReferencia.Text.Trim() + ".jpg";
                    if (System.IO.File.Exists(strFullFileName))
                    {
                        Image img = new System.Drawing.Bitmap(strFullFileName);
                        imgProd.Image = img;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    txtReferencia.SelectAll();
                }
            }
            #endregion
        }

        private void ConPreco_Load(object sender, EventArgs e)
        {
            lblTitulo.Text = dbConnection.Alias + " Consulta Preço";
            txtReferencia.Focus();
        }
    }
}
